package cn.huaqingcheng.tianshu.application.http;

import cn.huaqingcheng.tianshu.core.start.model.SiteGroup;
import cn.huaqingcheng.tianshu.core.start.param.SortParam;
import cn.huaqingcheng.tianshu.core.start.service.SiteGroupService;
import cn.huaqingcheng.tianshu.security.annotation.CurrentAccountId;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 站点分组
 */
@RestController
@RequiredArgsConstructor
public class SiteGroupController {

    private final SiteGroupService siteGroupService;


    /**
     * 查询所有分组
     */
    @GetMapping("/group")
    public List<SiteGroup> listGroup(@CurrentAccountId Long accountId) {
        return siteGroupService.getAll(accountId);
    }

    /**
     * 添加新分组
     */
    @PostMapping("/group")
    public Long createGroup(@RequestBody @Valid SiteGroup data, @CurrentAccountId Long accountId) {
        return siteGroupService.createGroup(data, accountId);
    }

    /**
     * 更新分组
     */
    @PutMapping("/group")
    public void updateGroup(@RequestBody @Valid SiteGroup data, @CurrentAccountId Long accountId) {
        siteGroupService.updateGroup(data, accountId);
    }

    /**
     * 更新分组排序
     */
    @PutMapping("/group/sort")
    public void updateSort(@RequestBody List<SortParam> params, @CurrentAccountId Long accountId) {
        siteGroupService.updateSort(params, accountId);
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/group/{id:\\d+}")
    public void remove(@PathVariable("id") Long id, @CurrentAccountId Long accountId) {
        siteGroupService.remove(id, accountId);
    }

}
